<template>
    <div class="wrapper">
		<div class="header">{{details.Tag_Profile_IM_Nick?details.Tag_Profile_IM_Nick:friendId}}</div>
		<div class="content">
			<div class="friend_img">
				<img src="img/face_1.png" id="userImg">
			</div>
			<div class="user_info" v-if="details.Tag_Profile_IM_SelfSignature">
					<div class="user_underline">
						<div class="items_left">
							<span class="items_title">个性签名</span>
							<span class="items_text">{{details.Tag_Profile_IM_SelfSignature}}</span>
						</div>
						<div class="items_right"><i class="iconfont icon-dayuhao"></i></div>
					</div>
			</div>
			<div class="user_info" v-if="details.Tag_Profile_IM_Nick">
					<div class="user_underline">
						<div class="items_left">
							<span class="items_title">昵称</span>
							<span class="items_text" id="userNick">{{details.Tag_Profile_IM_Nick}}</span>
						</div>
						<div class="items_right"><i class="iconfont icon-dayuhao"></i></div>
					</div>
			</div>
			<div class="user_info" v-if="details.Tag_Profile_IM_Gender">
					<div class="user_underline">
						<div class="items_left">
							<span class="items_title">性别</span>
							<span class="items_text" id="userSex">{{details.Tag_Profile_IM_Gender=='Gender_Type_Female'?'女':'男'}}</span>
						</div>
						<div class="items_right"><i class="iconfont icon-dayuhao"></i></div>
					</div>
			</div>
			<div class="user_info" v-if="details.Tag_Profile_IM_BirthDay">
					<div class="user_underline">
						<div class="items_left">
							<span class="items_title">生日</span>
							<span class="items_text">{{details.Tag_Profile_IM_BirthDay}}</span>
						</div>
						<div class="items_right"><i class="iconfont icon-dayuhao"></i></div>
					</div>
			</div>
			<div class="user_info" v-if="details.Tag_Profile_IM_Location">
					<div class="user_underline">
						<div class="items_left">
							<span class="items_title">地区</span>
							<span class="items_text">{{details.Tag_Profile_IM_Location}}</span>
						</div>
						<div class="items_right"><i class="iconfont icon-dayuhao"></i></div>
					</div>
			</div>
			<div class="user_info" v-if="details.Tag_Profile_IM_MsgSettings">
					<div class="user_underline">
						<div class="items_left">
							<span class="items_title">他的消息</span>
							<span class="items_text">{{details.Tag_Profile_IM_MsgSettings == 0?'接收消息':'不接收消息'}}</span>
						</div>
						<div class="items_right"><i class="iconfont icon-dayuhao"></i></div>
					</div>
			</div>
			<div class="details_btn">
				<router-link class="color_green" :to="{path: 'session', query:{id: friendId}}" tag="button">
					发送消息
                </router-link>
				<button class="color_green" @click="contrlFriend(isFriend)">{{isFriend?'删除好友':'加为好友'}}</button>
				<button class="color_red" @click.stop="contrlBlack(isBlack)">{{isBlack?'取消拉黑':'拉黑'}}</button>
			</div>
		</div>
	</div>
</template>

<script>
import $ from 'jquery';
import Client from "../../config/common";
export default {
    data(){
        return{
			loginInfo:{},
			friendId:Client.getQueryString('id'),
			details:{},//用户个人资料
			isFriend:false,//是否为本人好友
			BlackListItem:null,//黑名单列表
			isBlack:false//是否在我的黑名单中
        }
    },
    created(){
		Client.login((loginInfo) => {
			 this.loginInfo = loginInfo;
			 this.getProfile();
			 this.getAll(loginInfo);
			 this.getBlackList();
		});
	},
	mounted(){
		//读取缓存里的资料
	   /*console.log(window.localStorage);
	   let friendObj = Client.getStore('friendList');
	   Client.log(friendObj);
	   friendObj.forEach(ele => {
		   if(ele.Info_Account == this.friendId){
			   this.details = ele;
		   }
	   });
	   Client.log(this.details)*/
	   console.log('this.BlackListItem',this.BlackListItem);
    },

    components:{
       // headTop
    },

    computed:{

    },

    methods:{
		//拉取我的好友列表
		getAll(loginInfo) {	
				//Client.log(loginInfo);
			let getAllFriend = (cbOK, cbErr) => {
			let options = {
				From_Account: loginInfo.identifier,
				TimeStamp: 0,
				StartIndex: 0,
				GetCount: 100, //totalCount,
				LastStandardSequence: 0,
				TagList: [
					"Tag_Profile_IM_Nick",
					"Tag_Profile_IM_SelfSignature", //个性签名
					"Tag_Profile_IM_Image", //头像
					"Tag_Profile_IM_Gender", //性别
					"Tag_Profile_IM_AllowType", //加好友验证方式
					"Tag_Profile_IM_Location", //所在地
					"Tag_Profile_IM_BirthDay", //生日
					"Tag_Profile_IM_MsgSettings" //消息设置 置0表示接收消息，置1则不接收消息
				]
				};
				webim.getAllFriend(
				options,
				resp => {
					Client.log(resp);
					if (resp.FriendNum > 0) {
						let friendList = resp.InfoItem;
						friendList.forEach(ele => {
							if(this.friendId == ele.Info_Account){
								this.isFriend = true;
							}
						});
					}
				},
				err => {
					alert(err.ErrorInfo);
				}
				);
			};
			getAllFriend();
	    },
		//获取用户个人资料
		getProfile(loginInfo) {
			let tag_list = [
				"Tag_Profile_IM_Nick", //昵称
				"Tag_Profile_IM_Gender", //性别
				"Tag_Profile_IM_AllowType", //加好友方式
				"Tag_Profile_IM_Image" //头像
			];
			let objs = {
				To_Account: [this.friendId],
				TagList: tag_list
		};
		webim.getProfilePortrait(
			objs,
			resp => {
			console.log('用户个人资料',resp);
			if (resp.UserProfileItem && resp.UserProfileItem.length > 0) {
				let userInfos = resp.UserProfileItem[0];

				if (userInfos.ProfileItem) {
					let sonItem = userInfos.ProfileItem;
					sonItem.forEach((ele, index) => {
					userInfos[ele.Tag] = ele.Value;
				});
				this.details = userInfos;
				Client.log(this.details);
				}
			}

			},
			err => {
			alert(err.ErrorInfo);
			}
		);
		},
		//处理加好友还是删除好友
		contrlFriend(answer){
			if(answer){
				this.deleteFriend();
			}else{
				this.toAdd();
			}
		},
		//删除好友
		deleteFriend() {
			let options = {
				'From_Account': this.loginInfo.identifier,
				'To_Account': [this.friendId],
				//Delete_Type_Both'//单向删除："Delete_Type_Single", 双向删除："Delete_Type_Both".
				'DeleteType': "Delete_Type_Single"
			};
			webim.deleteFriend(
					options,
					function (resp) {
						console.log(resp)
					},
					function (err) {
						alert(err.ErrorInfo);
					}
			);
		},
		//添加好友
		toAdd() {
			const allowType = this.details.Tag_Profile_IM_AllowType;
			//判断对方是否允许加好友
			if (allowType == "AllowType_Type_NeedConfirm") {
				//需要验证
					const theNick = encodeURI(this.details.nick);
				//userInfo.nick = encodeURI(userInfo.nick);
					const img = this.details.Tag_Profile_IM_Image,
					id = this.details.To_Account,
					nick = this.details.Tag_Profile_IM_Nick;
					this.$router.push({path:'/friendCheck', query:{id,img,nick}});
			} else if (allowType == "AllowType_Type_DenyAny") {
				//拒绝任何人加加好友
				alert("该用户拒绝任何人添加好友");
			} else {
				//允许任何人
				//申请加好友
				let applyAddFriend = (() => {
				let add_friend_item = [
					{
					To_Account: this.friendId,
					AddSource: "AddSource_Type_Unknow",
					AddWording: "" //加好友附言，可为空
					}
				];
				let options = {
					From_Account: this.loginInfo.identifier,
					AddFriendItem: add_friend_item
				};

				webim.applyAddFriend(
					options,
					resp => {
					console.log(resp);
					if (resp.Fail_Account && resp.Fail_Account.length > 0) {
						for (let i in resp.ResultItem) {
						alert(resp.ResultItem[i].ResultInfo);
						break;
						}
					} else {
						alert("添加好友成功");
					}
					},
					err => {
						alert(err.ErrorInfo);
					}
				);
				})();
			}
		},
		//处理拉黑还是取消拉黑
		contrlBlack(answer){
			if(answer){
				this.deleteBlackList(this.friendId);
				this.isBlack = false;
			}else{
				this.pushBlack(this.friendId);
				this.isBlack = true;
			}
		},
		getBlackList(cbOK, cbErr) {
			let options = {
				'From_Account': this.loginInfo.identifier,
				'StartIndex': 0,
				'MaxLimited': 100,
				'LastSequence': 0
			};
			webim.getBlackList(
					options,
					(resp) => {
						console.log(resp)
						if(resp.BlackListItem&&resp.BlackListItem.length>0){
							this.BlackListItem = resp.BlackListItem;
							this.BlackListItem.forEach(ele => {
								if(ele.To_Account == this.friendId){
									this.isBlack = true;
								}
							});
						}
					},
					(err) => {
						alert(err.ErrorInfo);
					}
			);
		},
		//删除黑名单
		deleteBlackList(id) {
			var options = {
				'From_Account': this.loginInfo.identifier,
				'To_Account': [id]
			};
			webim.deleteBlackList(
					options,
					resp => {
						//console.log(resp);
						alert('取消拉黑成功');
					},
					err => {
						alert(err.ErrorInfo);
					}
			);
		},
		pushBlack(account){
		//添加黑名单
			let options = {
				'From_Account': this.loginInfo.identifier,
				'To_Account': [account]
			};
			webim.addBlackList(
					options,
					(resp) => {
						alert('拉黑成功');
					},
					(err) => {
						alert(err.ErrorInfo);
					}
			);
		}
    },
}

</script>

<style lang="scss"> 
   @import '../../style/friend_details';
</style>